/*
 * This file is part of aion-engine <aion-engine.com>
 *
 * aion-engine is private software: you can redistribute it and or modify
 * it under the terms of the GNU Lesser Public License as published by
 * the Private Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * aion-engine is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser Public License for more details.
 *
 * You should have received a copy of the GNU Lesser Public License
 * along with aion-engine.  If not, see <http://www.gnu.org/licenses/>.
 */
package com.aionengine.gameserver.network.aion.clientpackets;

import com.aionengine.gameserver.model.gameobjects.player.Player;
import com.aionengine.gameserver.model.items.storage.StorageType;
import com.aionengine.gameserver.model.team.legion.Legion;
import com.aionengine.gameserver.model.team.legion.LegionHistoryType;
import com.aionengine.gameserver.model.team.legion.LegionMember;
import com.aionengine.gameserver.model.team.legion.LegionPermissionsMask;
import com.aionengine.gameserver.network.aion.AionClientPacket;
import com.aionengine.gameserver.network.aion.AionConnection.State;
import com.aionengine.gameserver.network.aion.serverpackets.SM_SYSTEM_MESSAGE;
import com.aionengine.gameserver.services.LegionService;
import com.aionengine.gameserver.utils.PacketSendUtility;

/**
 * @author ATracer
 */
public class CM_LEGION_WH_KINAH extends AionClientPacket {

    public CM_LEGION_WH_KINAH(int opcode, State state, State... restStates) {
        super(opcode, state, restStates);
    }

    private long amount;
    private int operation;

    @Override
    protected void readImpl() {
        this.amount = readQ();
        this.operation = readC();
    }

    @Override
    protected void runImpl() {
        final Player activePlayer = getConnection().getActivePlayer();

        Legion legion = activePlayer.getLegion();
        if (legion != null) {
            LegionMember LM = LegionService.getInstance().getLegionMember(activePlayer.getObjectId());
            switch (operation) {
                case 0:
                    if (!LM.hasRights(LegionPermissionsMask.WH_WITHDRAWAL)) {
                        // You do not have the authority to use the Legion warehouse.
                        PacketSendUtility.sendPacket(activePlayer, new SM_SYSTEM_MESSAGE(1300322));
                        return;
                    }
                    if (activePlayer.getStorage(StorageType.LEGION_WAREHOUSE.getId()).tryDecreaseKinah(amount)) {
                        activePlayer.getInventory().increaseKinah(amount);
                        LegionService.getInstance().addHistory(legion, activePlayer.getName(), LegionHistoryType.KINAH_WITHDRAW, 2, Long.toString(amount));
                    }
                    break;
                case 1:
                    if (!LM.hasRights(LegionPermissionsMask.WH_DEPOSIT)) {
                        // You do not have the authority to use the Legion warehouse.
                        PacketSendUtility.sendPacket(activePlayer, new SM_SYSTEM_MESSAGE(1300322));
                        return;
                    }
                    if (activePlayer.getInventory().tryDecreaseKinah(amount)) {
                        activePlayer.getStorage(StorageType.LEGION_WAREHOUSE.getId()).increaseKinah(amount);
                        LegionService.getInstance().addHistory(legion, activePlayer.getName(), LegionHistoryType.KINAH_DEPOSIT, 2, Long.toString(amount));
                    }
                    break;
            }
        }
    }
}
